﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebManager;
using Maticsoft.DBUtility;//请先添加引用
namespace BaoJian.WebManager.Manager.RelateCol
{
    public partial class SetDefaultAD : System.Web.UI.Page
    {
        LYQ.BLL.DefaultPageAD bll = new LYQ.BLL.DefaultPageAD();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRegion();
                BindGridView();
            }
        }

        private void BindRegion()
        {
            LYQ.BLL.S_Group groupbll = new LYQ.BLL.S_Group();
            Region.DataSource = groupbll.GetList("GName !='系统管理员'").Tables[0];
            Region.DataBind();
        }
        private void BindGridView()
        {
            DataSet ds = bll.GetList("Region='" + Region.SelectedValue + "' order by Sequence asc");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        protected void Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/List.aspx";
            Page.Response.Redirect(url);
        }

        protected void NewAD_Click(object sender, EventArgs e)
        {          
            LYQ.Model.DefaultPageAD model = new LYQ.Model.DefaultPageAD();
            model.Image = txtImage.Text;
            model.Sequence =Convert.ToInt32(txtSequence.Text);
            model.Region = Region.SelectedValue;
            model.Description = txtDescription.Text;
            model.Type = chkType.Checked;
            model.URL = txtURL.Text;
            model.Height = Convert.ToInt32(txtHeight.Text);
            model.Width = Convert.ToInt32(txtWidth.Text);

            bll.Add(model);

            txtImage.Text = string.Empty;
            txtSequence.Text = string.Empty;
            txtDescription.Text = string.Empty;
            chkType.Checked = false;
            txtURL.Text = string.Empty;
            txtWidth.Text = string.Empty;
            txtHeight.Text = string.Empty;
            BindGridView();
        }

        protected void DeleteAD_Click(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Form["selectCB"] == null) return;
            string[] sel = Request.Form["selectCB"].Split(',');
         
            foreach (string d in sel)
            {
                bll.Delete(Convert.ToInt32(d));         
            }
            BindGridView();

        }

        protected void Upload_Click(object sender, EventArgs e)
        {
            string savePath = @"\uploads\ad\"+Region.SelectedValue+@"\";
            string imagePath = savePath.Replace(@"\","/");
            savePath = Server.MapPath(savePath);
            if (FileUpload1.HasFile)
            {
                savePath += FileUpload1.FileName;
                imagePath += FileUpload1.FileName;
                FileUpload1.SaveAs(savePath);
                txtImage.Text = imagePath;
            }
            else
            {
                // Notify the user that a file was not uploaded.
                UploadStatusLabel.Text = "文件上传失败";
            }
        }
    }
}
